package com.demo.gaoxiao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.gaoxiao.entity.Admin;
import com.demo.gaoxiao.service.IAdminService;
import com.demo.gaoxiao.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-06
 */
@Controller
public class AdminController {


    @Autowired
    private IAdminService iAdminService;

    /**
     * 资源路径
     */
    public String view="/view/admin";

    /**
     * 系统登陆
     * @param request
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/api/admin/login")
    @ResponseBody
    public Result login(HttpServletRequest request){
        String account = request.getParameter("account");
        if(StringUtils.isEmpty(account)){
            return Result.reError("账号不能为空");
        }
        String pwd =  request.getParameter("pwd");
        if(StringUtils.isEmpty(pwd)){
            return Result.reError("密码不能为空");
        }
        //进行登录认证
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("pwd",pwd);
        Admin admin=this.iAdminService.getOne(queryWrapper);
        if(admin==null){
            return Result.reError("账号密码错误！！！");
        }
        request.getSession().setAttribute("admin",admin);
        return Result.reOk("登录成功");
    }


    /**
     * 进入首页
     * @return
     */
    @RequestMapping("/")
    public String login(){
        return "/login";
    }



    /**
     * 进入首页
     * @return
     */
    @RequestMapping("/api/admin/index")
    public String index(){
        return view+"/index";
    }

    /**
     * 进入首页
     * @return
     */
    @RequestMapping("/api/admin/welcome")
    public String welcome(){
        return view+"/welcome";
    }



}
